﻿namespace YmtAuth.Web.Service
{
    using ServiceStack;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using YmtAuth.Dto;
    using Ymatou.User.Dto.Auth;
    using Ymatou.User.Dto.PasswordAction;
    using ServiceStack.Host;
    using ServiceStack.Web;

    public class AppServiceHost : AppHostBase
    {
        public AppServiceHost()
            : base("Ymatou.UserAuthSystem", typeof(AppServiceHost).Assembly
            )
        {

        }

        public override void Configure(Funq.Container container)
        {
            Routes
                //.Add<UserLoginByCookieRequestDto>("/auth/token/{UserId}/{UserType}/{Token}/{ClientIp}/{UpdateToken}", "GET") //用户从cookie登录
                .Add<LoginAuthRequestDto>("/auth/loginauth/{LoginId}/{Pas}/{ClientIp}/{Description}/{SourceType}", "GET")
                .Add<UserRegisterRequestDto>("/auth/register", "POST")
                .Add<PasswordRequestDto>("/auth/changpas", "POST")
                //.Add<UserLoginTokenRequestDto>("/auth/uptoken", "POST") //更新登录token                
                .Add<LoginLimitRequestDto>("/auth/loginexceedlimit/{LoginId}/{TimeSpane}", "GET") //用户登录是否超限
                .Add<UserBillsRequestDto>("/auth/billsfind/{UserId}", "GET")
                .Add<UserBillsRequestDto>("/auth/billsupdate", "POST") //凭据更新
                .Add<UserBillsExistsRequestDto>("/auth/existsfind/{LoginId}/{UserId}", "GET") //凭据存在性查询
                .Add<UserBillsExistsRequestDto>("/auth/existsfind/{EMail}/{UserId}", "GET")
                .Add<UserBillsExistsRequestDto>("/auth/existsfind/{Mobile}/{UserId}", "GET")
                .Add<SignAuthRequestDto>("/auth/findtoken/{Token}", "GET") //SSO token 获取用户ID
                .Add<SignAuthRequestDto>("/auth/checksign", "POST") //SSO 签名校验
                .Add<BillsLoginIdModifyEventRequestDto>("/auth/event/loginid/find/{LoginId}/{ActionsSource}", "GET")
                .Add<UserTokenDto>("/auth/createtoken", "POST")
                .Add<SSOTokenDto>("/auth/logout", "POST")
                .Add<UserRemoveTokenRequestDto>("/auth/removetoken", "POST")
                .Add<LockUserRequestDto>("/auth/user/locked/", "POST")
                .Add<UnLockUserRequestDto>("/auth/user/unlocked/", "POST")
                .Add<LockIpRequestDto>("/auth/userip/locked/", "POST")
                .Add<UnLockIpRequestDto>("/auth/userip/unlocked/", "POST")
                .Add<UserLockStatusRequestDto>("/auth/user/lockstatus/", "GET")
                .Add<IpLockStatusRequestDto>("/auth/userip/lockstatus/", "GET")
                .Add<IpAreaAuthStatusRequestDto>("/auth/ipareaauth/", "POST")
                .Add<DeviceAuthStatusRequestDto>("/auth/deviceauth/","POST")
                .Add<GetUserBillsRequestDto>("/auth/userbills/find/{LoginId}/{UserId}/{IncludeLoginPas}/{IncludeTps}", "GET")
                .Add<LoginIdUpdateRequestDto>("/auth/LoginIdUpdate/","POST")
                ;

            SetConfig(new HostConfig
            {
#if DEBUG
                DebugMode = true,
#endif

            });

            //Handle Exceptions occurring in Services:           

            AppHostBase.Instance.ServiceExceptionHandlers.Add((httpReq, request, exception) =>
            {
                YmtSystem.CrossCutting.YmatouLoggingService.Error("{0} 请求,错误 {1}", request.ToJson(), exception.ToString());
                //log your exceptions here
                //...
                //call default exception handler or prepare your own custom response
                return DtoUtils.CreateErrorResponse(request, exception, new ResponseStatus { ErrorCode = "500", Message = exception.ToString() });
            });

            //Handle Unhandled Exceptions occurring outside of Services
            //E.g. in Request binding or filters:
            //AppHostBase.Instance.ExceptionHandler = (req, res, operationName, ex) =>
            //{
            //    res.Write("Error: {0}: {1}".Fmt(ex.GetType().Name, ex.Message));
            //    res.EndRequest(skipHeaders: true);
            //};
        }

        public override IServiceRunner<TRequest> CreateServiceRunner<TRequest>(ActionContext actionContext)
        {
            return new InterceptServiceStackService<TRequest>(this, actionContext);
        }

        public static void Start()
        {
            (new AppServiceHost()).Init();
        }
    }
}